package org.shopen.ai.puremvc.view.project
{
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	import org.shopen.ai.puremvc.ApplicationConstants;
	import org.shopen.ai.puremvc.model.algo.AlgoProxy;
	import org.shopen.ai.puremvc.model.settings.AutoSettings;
	import org.shopen.ai.puremvc.model.settings.ICommitSettingsResult;
	import org.shopen.ai.puremvc.model.settings.SettingsProxy;
	import org.shopen.ai.puremvc.view.project.components.AutomaticSetupView;
	import org.shopen.ai.puremvc.view.project.events.DataChangedEvent;

	public class AutomaticSetupViewMediator extends Mediator
	{
		public static const NAME : String = "AutomaticSetupViewMediator";
		
		public function get autoSetupView() : AutomaticSetupView
		{
			var result : AutomaticSetupView = viewComponent as AutomaticSetupView;
			return result;
		}
		
		public function AutomaticSetupViewMediator(viewComponent : AutomaticSetupView = null)
		{
			super(AutomaticSetupViewMediator.NAME, viewComponent);
		}
		
		override public function getMediatorName():String
		{
			return AutomaticSetupViewMediator.NAME;
		}
		
		override public function onRegister():void
		{
			initializeView();
			addEventListeners();
			sendNotification(ApplicationConstants.INITIALIZE_AUTOMATIC_SETTINGS, null, ApplicationConstants.AUTO_SETTINGS_TYPE);
		}
		
		protected function initializeView() : void
		{
			autoSetupView.algosLabelField = "title";
		}
		
		protected function addEventListeners() : void
		{
			autoSetupView.addEventListener(AutomaticSetupView.REPEATCOUNT_CHANGED, onRepeatCountChanged);
			autoSetupView.addEventListener(AutomaticSetupView.SEARCHALGOS_CHANGED, onSearchAlgosChanged);
		}
				
		private function onRepeatCountChanged(event : DataChangedEvent) : void
		{
			commitSetting(getSettings());
		}
		
		private function onSearchAlgosChanged(event : DataChangedEvent) : void
		{
			commitSetting(getSettings());
		}
		
		protected function getSettings() : AutoSettings
		{
			var selectedAlgos : ArrayCollection = new ArrayCollection(autoSetupView.selectedAlgos);
			var repeatCount : int = autoSetupView.repeatCount;
			var settings : AutoSettings = new AutoSettings(selectedAlgos, repeatCount);
			return settings;
		}
		
		protected function setSettings(settings : AutoSettings) : void
		{
			autoSetupView.selectedAlgos = settings.algorithms.toArray();
			autoSetupView.repeatCount = settings.repeatCount;
		}
		
		protected function commitSetting(settings : AutoSettings) : void
		{
			sendNotification(ApplicationConstants.COMMIT_AUTOMATIC_SETTINGS, settings);
		}
		
		override public function listNotificationInterests():Array
		{
			return [AlgoProxy.ALGORITHMS_LOADED,
					SettingsProxy.AUTOSETTINGS_CHANGED,
					SettingsProxy.AUTOSETTINGS_LOADED,
					SettingsProxy.COMMIT_AUTOSETTINGS_RESULT];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			var name : String = notification.getName();
			var type : String = notification.getType();
			switch (name)
			{
				case AlgoProxy.ALGORITHMS_LOADED:
					if (type == ApplicationConstants.AUTO_SETTINGS_TYPE)
					{
						this.autoSetupView.algosDataSource = notification.getBody() as ArrayCollection;
					}
					break;
				case SettingsProxy.AUTOSETTINGS_LOADED:
				case SettingsProxy.AUTOSETTINGS_CHANGED:
					setSettings(notification.getBody() as AutoSettings);
					break;
				case SettingsProxy.COMMIT_AUTOSETTINGS_RESULT:
					var commitResult : ICommitSettingsResult = notification.getBody() as ICommitSettingsResult;
					autoSetupView.setValidation(commitResult.isValid, commitResult.toString());
					break;
			}
		}
	}
}